/*******************************************************************************
 * Copyright (c) 2010, 2017 西安秦晔信息科技有限公司
 * Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *******************************************************************************/
package com.qinyeit.webapp.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.util.Date;
import java.util.Map;

import static com.qinyeit.serviceapp.security.PrincipalIdentifierKey.*;

/**
 * <p>ClassName: com.qinyetech.webapp.security.CurrentAccountUtils
 * <p>Function: 提供获取当前登录用户信息的工具方法
 * <p>date: 2017-10-27 14:13
 *
 * @author wuqing
 * @version 1.0
 * @since JDK 1.8
 */
public class CurrentAccountUtils {

    /**
     * 获取认证过的账号
     * @return
     */
    public static String getAuthenticatedAccountSecurityKey(){
        //获取当前登录的账号
        return getPrincipal().get(PRINCIPAL_SECURITY_KEY).toString();
    }

    /**
     * 获取认证过的账号
     * @return
     */
    public static String getAuthenticatedAccountName(){
        //获取当前登录的账号
        return getPrincipal().get(PRINCIPAL_ACCOUNT_NAME).toString();
    }

    /**
     * 获取认证过的账号真实姓名
     * @return
     */
    public static String getAuthenticatedAccountRealName(){
        //获取当前登录的账号
        return getPrincipal().get(PRINCIPAL_ACCOUNT_REAL_NAME).toString();
    }

    /**
     * 获取认证过的账号ID
     * @return
     */
    public static Long getAuthenticatedAccountID(){
        //获取当前登录的账号
        return (Long)getPrincipal().get(PRINCIPAL_ACCOUNT_ID);
    }

    /**
     * 获取最后一次登录时间，有可能为空
     * @return
     */
    public static Date getLastLoginDate(){
        return (Date)getPrincipal().get(PRINCIPAL_LAST_LOGIN_AT);
    }

    /**
     * 获取头像图片路径
     * @return
     */
    public static String getHeadPicUrl(){
        String headPic=(String)getPrincipal().get(PRINCIPAL_ACCOUNT_HEAD_URL);
        if(StringUtils.isBlank(headPic) || "default.png".equals(headPic)){
            return "/assets/app/images/management/default.png";
        }
        //return "/common/img/"+headPic;
        return headPic;  //图片路径存储为七牛绝对地址
    }

    /**
     * 获取真实姓名
     * @return
     */
    public static String getRealName(){
        String realName=(String)getPrincipal().get(PRINCIPAL_ACCOUNT_REAL_NAME);
        if(StringUtils.isBlank(realName)){
            return getAuthenticatedAccountName();
        }
        return realName;
    }

    @SuppressWarnings("unchecked")
    private static Map<String,Object> getPrincipal(){
        if(SecurityUtils.getSubject().isAuthenticated()){
           return (Map<String,Object>)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        }
        return null;
    }

    /**
     * 获取SessionId
     * @return
     */
    public static String getSessionId(){
        return SecurityUtils.getSubject().getSession().getId().toString();
    }

    /**
     * 获取当前登录的商户ID
     * @return
     */
    public static Long getAuthenticatedMerchantGroupID(){
        return (Long)getPrincipal().get(PRINCIPAL_MERCHANT_GROUP_ID);
//        return 1L;

    }

    /**
     * 获取当前登录的商户ID
     * @return
     */
    public static Long getAuthenticatedEmployeeId(){
        return (Long)getPrincipal().get(PRINCIPAL_EMPLOYEE_ID);
//        return 1L;

    }

    /**
     * 获取当前登录的商户ID
     * @return
     */
    public static String getAuthenticatedEmployeeName(){
        return (String)getPrincipal().get(PRINCIPAL_EMPLOYEE_NAME);
//        return 1L;
    }

    /**
     * 获取当前登录的账号 判断是否是管理员
     * @return
     */
    public static Boolean getAuthenticatedIsAdmin(){
        return (Boolean) getPrincipal().get(PRINCIPAL_IS_ADMIN);
        //return 1L;
    }
    /**
     * 获取当前登录的员工 可管理的门店 [2,3]
     * @return
     */
    public static String getAuthenticatedManageBranch(){
        return (String) getPrincipal().get(PRINCIPAL_EMPLOYEE_MANAGE_BRANCH);
        //return 1L;
    }
}
